package com.wholesale.utils;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.wholesale.R;
import com.wholesale.beans.OrderTrackingInfo;

public class TrackingPackagesListAdapter extends
		ArrayAdapter<OrderTrackingInfo> {

	// ListView中的数据来源
	private ListView listView;
	private static HashMap<Integer, Boolean> isSelected = new HashMap<Integer, Boolean>();;

	public TrackingPackagesListAdapter(Context context, ListView listView) {
		super(context, 0);
		this.listView = listView;
	}

	public TrackingPackagesListAdapter(Context context,
			List<OrderTrackingInfo> orderTrackingInfoList, ListView listView) {
		super(context, 0, orderTrackingInfoList);
		this.listView = listView;
		int size = orderTrackingInfoList.size();
		for (int i = 0; i < size; i++) {
			isSelected.put(i, false);
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		TrackingPackagesCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.mytrackingitem, null);
			viewCache = new TrackingPackagesCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (TrackingPackagesCache) rowView.getTag();
		}
		OrderTrackingInfo orderTrackingInfo = getItem(position);
		// Set the text on the TextView
		viewCache.getOrderNumber().setText(orderTrackingInfo.getOrderSn());
		viewCache.getShippingNumber().setText(orderTrackingInfo.getInvoiceNo());
		if (orderTrackingInfo.getOrderTime() != null) {
			viewCache.getOrderTime().setText(orderTrackingInfo.getOrderTime());
		}
		if (orderTrackingInfo.getPaymentTime() != null) {
			viewCache.getPaymentTime().setText(
					orderTrackingInfo.getPaymentTime());
		}
		if (orderTrackingInfo.getShippingTime() != null) {
			viewCache.getShippingTime().setText(
					orderTrackingInfo.getShippingTime());
		}
		viewCache.getShippingTape()
				.setText(orderTrackingInfo.getShippingName());

		viewCache.getShippingStatus().setText(
				orderTrackingInfo.getShippingStatusName());

		if (!orderTrackingInfo.getShippingStatusName().equalsIgnoreCase(
				"Unshipped")) {
			viewCache.getCopyButton().setVisibility(View.VISIBLE);
		} else {
			viewCache.getCopyButton().setVisibility(View.GONE);
		}

		return rowView;
	}

	/**
	 * 批量添加数据
	 * 
	 * @param items
	 */
	public void addItems(List<OrderTrackingInfo> items) {
		for (OrderTrackingInfo orderTrackingInfo : items) {
			this.add(orderTrackingInfo);
		}
	}

}
